/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MediumManagement;

import Utilities.Functions;
import Utilities.PermissionsConstants;
import org.virtualbox_3_2.AccessMode;

/**
 * Instances of this class are used by the user interface when a request to
 * open a hard disk is sent to the media management service.
 *
 * @author Angel Sanadinov
 */
public class OpenHardDiskRequest extends MediumManagementRequest
{
    private String location;        //the location of the disk on the host
    private AccessMode accessMode;  //the access mode of the disk (read/read+write)
    private boolean isImageIdSet;   //denotes whether an image id was set
    private boolean isParentIdSet;  //denotes whether a parent id was set
    private String imageId;         //explicit image id
    private String permissions;     //the permissions of the disk
    private String parentId;        //parent id

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public OpenHardDiskRequest()
    {
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_OPEN_HDD);
        this.location = null;
        this.accessMode = null;
        this.isImageIdSet = false;
        this.isParentIdSet = false;
        this.imageId = null;
        this.parentId = null;
    }

    /**
     * Constructs an "open hard disk" request with the specified data. <br><br>
     *
     * <b>Note:</b> <i>The hard disk image UUID is automatically set by VirtualBox.</i>
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the disk resides
     * @param location the location of the hard disk on the host system
     * @param accessMode the disk access mode
     * @param permissions permissions data for the hard disk
     */
    public OpenHardDiskRequest(int requestorId, int serverId, String location, AccessMode accessMode, String permissions)
    {
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_OPEN_HDD);
        setRequestorId(requestorId);
        setServerId(serverId);
        this.permissions = permissions;
        this.location = location;
        this.accessMode = accessMode;
        this.imageId = null;
        this.parentId = null;
        this.isImageIdSet = false;
        this.isParentIdSet = false;
    }

    /**
     * Retrieves the location of the hard disk on the host system.
     *
     * @return the hard disk location
     */
    public String getLocation()
    {
        return location;
    }

    /**
     * Retrieves the access mode of the disk.
     *
     * @return the disk's access mode
     */
    public AccessMode getAccessMode()
    {
        return accessMode;
    }

    /**
     * Retrieves the explicit hard disk image id.
     *
     * @return the image id, if available or <code>null</code> otherwise
     */
    public String getImageId()
    {
        return imageId;
    }

    /**
     * Retrieves the permissions of the new disk.
     *
     * @return the new disk's permissions
     */
    public String getPermissions()
    {
        return permissions;
    }

    /**
     * Retrieves the id of the parent of the new disk.
     *
     * @return the parent id, if available or <code>null</code> otherwise
     */
    public String getParentId()
    {
        return parentId;
    }

    /**
     * Returns whether the explicit image id was set.
     *
     * @return <code>true</code> if the id was set or <code>false</code> if it was not set
     */
    public boolean isImageIdSet()
    {
        return isImageIdSet;
    }

    /**
     * Returns whether the disk's parent id was set.
     *
     * @return <code>true</code> if the id was set or <code>false</code> if it was not set
     */
    public boolean isParentIdSet()
    {
        return isParentIdSet;
    }

    /**
     * Sets the location of the hard disk image.
     *
     * @param location the hard disk image location
     */
    public void setLocation(String location)
    {
        this.location = location;
    }

    /**
     * Sets the disk's access mode.
     *
     * @param accessMode the access mode
     */
    public void setAccessMode(AccessMode accessMode)
    {
        this.accessMode = accessMode;
    }

    /**
     * Sets the explicit hard disk image id.
     *
     * @param imageId the image id
     */
    public void setImageId(String imageId)
    {
        this.imageId = imageId;
        this.isImageIdSet = true;
    }

    /**
     * Sets the permissions of the new disk.
     *
     * @param permissions the new disk's permissions
     */
    public void setPermissions(String permissions)
    {
        this.permissions = permissions;
    }

    /**
     * Sets the id of the new disk's parent.
     *
     * @param parentId the parent id
     */
    public void setParentId(String parentId)
    {
        this.parentId = parentId;
        this.isParentIdSet = true;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && Functions.isLocationValid(location) && accessMode != null 
                && Functions.arePermissionsValid(permissions)
                && (!isImageIdSet || imageId != null) /* imageId can be an empty string but not null */
                && (!isParentIdSet || Functions.isUUIDValid(parentId)))
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (mediumId,location,accessMode,imageId,
     * permissions,parentId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return OpenHardDiskRequest.class.getSimpleName()
                + ": (" + getMediumId() + "," + location + "," + accessMode
                + "," + imageId + "," + permissions + "," + parentId + ")";
    }
}
